using System;
using System.Collections.Generic;
using System.Linq;
using BitMango.Diagnostics;
using JsonFx.Json;
using UnityEngine;

namespace BitMango
{
    public static class Alarm
    {
        static Alarm()
        {
            Alarm.Init();
        }

        public static int AlarmCount
        {
            get
            {
                return Alarm.entrys.Count;
            }
        }

        public static void Init()
        {
            if (PlayerPrefs.HasKey("Alarm"))
            {
                string @string = PlayerPrefs.GetString("Alarm");
                Alarm.entrys = JsonReader.Deserialize<Alarm.Entry[]>(@string).ToList<Alarm.Entry>();
            }
        }

        public static void Register(string key, string title, string message, DateTime alarmTime, bool isSoundEnabled = false)
        {
            TimeSpan timeSpan = alarmTime - DateTime.Now;
            if (timeSpan.TotalHours > 0.0)
            {
                Alarm.Register(key, title, message, timeSpan.TotalHours, isSoundEnabled);
            }
        }

        public static void Register(string key, string title, string message, double intervalHours, bool isSoundEnabled = false)
        {
            Alarm.Unregister(key);
            key = Alarm.PREFIX + key;
            DateTime date = DateTime.Now.AddHours(intervalHours);
            int value = Alarm.RegisterInternal(title, message, date, isSoundEnabled);
            PlayerPrefs.SetInt(key, value);
            PlayerPrefs.Save();
        }

        private static int RegisterInternal(string title, string message, DateTime date, bool isSoundEnabled = false)
        {
            Debugger.Assert(title.Length > 0 && message.Length > 0);
            Alarm.entrys.Sort((Alarm.Entry a, Alarm.Entry b) => a.index.CompareTo(b.index));
            int num = 0;
            while (Alarm.entrys.Count > num && Alarm.entrys[num].index == num)
            {
                num++;
            }
            int num2 = num;
            Alarm.Entry entry = new Alarm.Entry();
            entry.index = num2;
            entry.title = title;
            entry.msg = message;
            entry.date = date;
            Alarm.entrys.Add(entry);
            PlayerPrefs.SetString("Alarm", JsonWriter.Serialize(Alarm.entrys));
            return num2;
        }

        public static void Unregister(string key)
        {
            key = Alarm.PREFIX + key;
            if (!PlayerPrefs.HasKey(key))
            {
                return;
            }
            int @int = PlayerPrefs.GetInt(key);
            Alarm.UnregisterInternal(@int);
            PlayerPrefs.DeleteKey(key);
            PlayerPrefs.Save();
        }

        private static void UnregisterInternal(int index)
        {
            Debugger.Assert(index >= 0);
            Debugger.Assert(Alarm.entrys != null);
            List<Alarm.Entry> unregisterEntrys = Alarm.GetUnregisterEntrys(index);
            if (unregisterEntrys == null)
            {
                return;
            }
            foreach (Alarm.Entry item in unregisterEntrys)
            {
                Alarm.entrys.Remove(item);
            }
            PlayerPrefs.SetString("Alarm", JsonWriter.Serialize(Alarm.entrys));
        }

        public static void AllUnregister()
        {
            Alarm.entrys.RemoveRange(0, Alarm.entrys.Count);
            PlayerPrefs.SetString("Alarm", JsonWriter.Serialize(Alarm.entrys));
        }

        private static List<Alarm.Entry> GetUnregisterEntrys(int index)
        {
            Debugger.Assert(Alarm.entrys != null, "Alarm.GetUnregisterEntrys : entrys is null");
            List<Alarm.Entry> list = new List<Alarm.Entry>();
            foreach (Alarm.Entry entry in Alarm.entrys)
            {
                Debugger.Assert(entry != null, "Alarm.GetUnregisterEntrys : entry is null");
                Debugger.Assert(true, "Alarm.GetUnregisterEntrys : entry.index is null");
                if (entry != null)
                {
                    if (entry.index == index)
                    {
                        list.Add(entry);
                    }
                }
            }
            return list;
        }

        public static List<Alarm.Entry> entrys = new List<Alarm.Entry>();

        private static string PREFIX = "BM_ALARM_";

        public class Entry
        {
            public int index;

            public string title = string.Empty;

            public string msg = string.Empty;

            public DateTime date = DateTime.Now;
        }
    }
}
